package com.yc.thread.rejectedPolicy;

import java.util.Scanner;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Title: 测试线程的拒绝策略 <br>
 * Description: <br>
 * Author:陈圆(chenyuan@bosssoft.com.cn) <br>
 * Date:2020/8/6
 */
public class ThreadPoolExecutorRejectedDemo {

    /***
     * 线程的拒绝策略一共默认的四种，我们也可以通过继承RejectedExecutionHandler接口并实现rejectedExecution方法进行重写
     * 源码部分
     * 策略1：
     *     public static class CallerRunsPolicy implements RejectedExecutionHandler {
     *
     *         public CallerRunsPolicy() { }
     *
     *         @Override
     *         public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
     *              // 由调用线程处理该任务
     *             if (!e.isShutdown()) {
     *                 r.run();
     *             }
     *         }
     *     }
     * 策略2：
     *     public static class AbortPolicy implements RejectedExecutionHandler {
     *
     *         public AbortPolicy() {
     *         }
     *
     *         @Override
     *         public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
     *             // 丢弃任务，并抛出异常
     *             throw new RejectedExecutionException("Task " + r.toString() +
     *                     " rejected from " +
     *                     e.toString());
     *         }
     *     }
     * 策略3：
     *      public static class DiscardPolicy implements RejectedExecutionHandler {
     *         public DiscardPolicy() { }
     *         // 啥也不干，就是丢弃任务
     *         @Override
     *         public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
     *         }
     *      }
     *  策略4：
     *      public static class DiscardOldestPolicy implements RejectedExecutionHandler {
     *
     *         public DiscardOldestPolicy() { }
     *         // 丢弃最前面的任务，重新尝试执行任务
     *         @Override
     *         public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
     *             if (!e.isShutdown()) {
     *                 // 丢弃队列最前面的任务
     *                 e.getQueue().poll();
     *                 // 重新尝试执行任务
     *                 e.execute(r);
     *             }
     *         }
     *     }
     */

    public static void main(String[] args) {
        testRejected();
    }


    /***
     * Description:测试线程池的拒绝策略 <br>
     * Author: 陈圆(chenyuan@bosssoft.com.cn) <br>
     * Date:2020/8/6
     * @param
     * @return void
     */
    public static void testRejected() {
        int corePoolSize = 1;
        int maxmumPoolSize = 2;
        long keepAliveTime = 10L;
        TimeUnit unit = TimeUnit.SECONDS;
        final LinkedBlockingQueue linkedBlockingQueue = new LinkedBlockingQueue(2);
        RejectedExecutionHandler handler = null;
        boolean flag = true;
        Scanner scanner = new Scanner(System.in);
        char key  = ' ';
        while (flag) {
            System.out.println("1，2，3，4分别表示策略1，2，3，4。 0退出");
            key = scanner.next().charAt(0);
            boolean execute = true ;
            switch (key) {
                case '1':
                    handler =  new ThreadPoolExecutor.CallerRunsPolicy();
                    break;
                case '2':
                    handler = new ThreadPoolExecutor.AbortPolicy();
                    break;
                case '3':
                    handler = new ThreadPoolExecutor.DiscardPolicy();
                    break;
                case '4':
                    handler = new ThreadPoolExecutor.DiscardOldestPolicy();
                case '0':
                    flag = false;
                    execute = false;
                    break;
                default:
                    execute = false;
                    System.out.println("输入有误,请重新输入");
                    break ;
            }
            if(execute){
                final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, maxmumPoolSize, keepAliveTime, unit, linkedBlockingQueue, handler);

                System.out.println("主线程名字：" + Thread.currentThread().getName());
                for (int i = 0; i <= 10; i++) {
                    final int num = i;
                    threadPoolExecutor.execute(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                Thread.sleep(2000L);
                                System.out.println("当前线程名称为：" + Thread.currentThread().getName() + "编号为：" + num);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
                threadPoolExecutor.shutdown();
            }
        }
        System.out.println("程序退出!");

//        // 策略1
//        RejectedExecutionHandler callerRunsPolicy = new ThreadPoolExecutor.CallerRunsPolicy();
//        // 策略2
//        RejectedExecutionHandler abortPolicy = new ThreadPoolExecutor.AbortPolicy();
//        // 策略3
//        RejectedExecutionHandler disCardPolicy = new ThreadPoolExecutor.DiscardPolicy();
//        // 策略4
//        RejectedExecutionHandler discardOldestPolicy = new ThreadPoolExecutor.DiscardOldestPolicy();



    }


}
